<?php
declare (strict_types=1);

namespace app\controller\api\v1\user\mine;

use app\service\logic\common\user\InviteService;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
use Endroid\QrCode\Label\Font\NotoSans;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\App;
use think\response\Json;
use app\controller\api\ApiController;

class InviteController extends ApiController
{
    protected $service;

    public function __construct(App $app, InviteService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * 邀请二维码
     *
     * @return Json
     */
    public function qr()
    {
        $params = request()->post();

        if (isset($params['uid'])) {
            $userId = $params['uid'];
        } else {
            $user = auth('user')->userOrFail();
            $userId = $user['id'];
        }

        $domain = config('domain.api');
        $url = $domain . '/pages/index/index?from=' . $userId;

        // 二维码地址
        $qr = Builder::create()
            ->writer(new PngWriter())
            ->writerOptions([])
            ->data($url)
            ->encoding(new Encoding('UTF-8'))
            ->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
            ->size(300)
            ->margin(10)
            ->roundBlockSizeMode(new RoundBlockSizeModeMargin())
            ->labelText('')
            ->labelFont(new NotoSans(20))
            ->labelAlignment(new LabelAlignmentCenter())
            ->build();
        $relative = 'storage/QR/qr_' . $userId . '.png';
        $path = public_path() . $relative;
        $qr->saveToFile($path);

        return amis([
            'src' => config('domain.api') . '/' . $relative,
        ]);
    }

    // /**
    //  * 小程序邀请码
    //  *
    //  * @return Json
    //  */
    // public function wechatQr()
    // {
    //     $params = request()->post();
    //
    //     if (isset($params['uid'])) {
    //         $userId = $params['uid'];
    //     } else {
    //         $user = auth('api')->userOrFail();
    //         $userId = $user['id'];
    //     }
    //
    //     $result = WechatFacade::getwxacodeunlimit(['scene' => 'from=' . $userId . '&type=1']);
    //     $relative = 'storage/qr/qr' . $userId . '.png';
    //     file_put_contents($relative, $result);
    //
    //     return amis([
    //         'src' => config('domain.api') . '/' . $relative,
    //     ]);
    // }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $user = auth('user')->userOrFail();
        $search['from'] = $user['id'];

        $fun = function ($model) {
            if (!empty($model['to_mobile'])) {
                $model['to_mobile'] = substr($model['to_mobile'], 0, 3) . '****' . substr($model['to_mobile'], -4);
            }
            unset($model['belongToUser']);
            return $model;
        };
        $result = $this->service->getList($params, $search_fields, $search, ['belongToUser'], $fun);
        return amis($result);
    }
}
